class CreatePermissions < ActiveRecord::Migration
	
	def self.up
		create_table :permissions do |t|
			t.string :key, null: false
			t.string :created_by
			t.string :updated_by

			t.timestamps
		end

		add_index :permissions, :key, unique: true

		Permission.create_translation_table! description: :text

		create_table :permissions_user_groups, :id => false do |t|
			t.integer :permission_id
			t.integer :user_group_id
		end

		add_index :permissions_user_groups, [:permission_id, :user_group_id], unique: true, 
			name: 'index_permission_id_and_user_group_id'

		create_table :permissions_users, :id => false do |t|
			t.integer :permission_id
			t.integer :user_id
		end

		add_index :permissions_users, [:permission_id, :user_id], unique: true
	end
	
	def self.down
		drop_table :permissions_users
		drop_table :permissions_user_groups
		Permission.drop_translation_table!
		drop_table :permissions
	end
end
